/***
App prototype JS for global function
@author zhaidw
**/
function app_title(title){ document.title= title +  ' - ' +  document.title;  }
function app_alert(msg,callback){ alert(msg);}
function app_confirm(msg,callback){ return confirm(msg);}
function app_prompt(title,default_value,callback){ return window.prompt(title,default_value);}
function app_url_encode(msg,callback){ return encodeURIComponent(msg);}

//调试用，firebug的控制台输出
function app_log(msg){
	try{
		var hasConsole = Boolean(window.console && window.console.firebug);
		if(hasConsole && 'yes' == app.log_enabled ){
			console.debug(msg);
			$Log.loginfo(msg);
		}else{
			hasConsole = Boolean(window.console && window.console.debug);
			if(hasConsole && 'yes' == app.log_enabled ){
				console.debug(msg);
				$Log.loginfo(msg);
			}
		}
	}catch(e){}
};



String.prototype.endWith=function(str){
	if(str==null||str==""||this.length==0||str.length>this.length)
		return false;
	if(this.substring(this.length-str.length)==str)
		return true;
	else
		return false;
	return true;
};

String.prototype.startWith=function(str){
	if(str==null||str==""||this.length==0||str.length>this.length)
		return false;
	if(this.substr(0,str.length)==str)
		return true;
	else
		return false;
	return true;
};

Date.prototype.format = function(format)
{
    var e = ["星期天","星期一","星期二","星期三","星期四","星期五","星期六"];
    var o =
    {
        "M+" : this.getMonth()+1,                  //month
        "d+" : this.getDate(),                     //day
        "h+" : this.getHours(),                    //hour
        "m+" : this.getMinutes(),                  //minute
        "s+" : this.getSeconds(),                  //second
        "q+" : Math.floor((this.getMonth()+3)/3), //quarter
        "S" : this.getMilliseconds()               //millisecond
    };
    
    if(/(y+)/.test(format))
    format=format.replace(RegExp.$1,(this.getFullYear()+"").substr(4 - RegExp.$1.length));
    if(/(e)/.test(format)) format=format.replace(RegExp.$1,e[this.getDay()]);
    for(var k in o)
    if(new RegExp("("+ k +")").test(format))
    format = format.replace(RegExp.$1,RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
    return format;
};


app_get_date = function(days){
	var aday = 3600 * 24 * 1000;
	//app_log('app.timestamp: ' +  app.timestamp);
	
	var millisec = new Date().getTime();
	if(days){
		millisec = millisec + ( aday * days);
	}
	var d = new Date(millisec);
	
	//app_log('d: ' + d);
	
	return d.format('yyyy-MM-dd');
};

app_replace_domain = function(val){
	var orgValue = val;
	var url = orgValue;
	
	if(url){
		url = url.toLowerCase();
		
		if( url.startWith('http://') || url.startWith('https://') ){
			var pos = url.indexOf('/',8);
			if( pos > -1){
				orgValue = orgValue.substr(pos, url.length);
			}
			
		}
		
	}

	
	return orgValue;
};